package com.jy.shorturl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jy.shorturl.service.impl.ShortUrlServiceImpl;
import com.jy.shorturl.util.ShortString;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("api")
public class ShortUrlController {
	
	@Autowired
	private ShortUrlServiceImpl ssi;
	
	@Value("${URL}")
	private String url;
	
	@Autowired
	private ShortString ss;
	
	@ApiOperation(value="插入短链接", notes="向数据库中插入全部短链接排列组合，大概14亿条") 
	@PutMapping("insertShortUrl")
	public String insertShortUrl(){
		
		try {
			ss.per(new char[1],0);
			ss.per(new char[2],1);
			ss.per(new char[3],2);
			ss.per(new char[4],3);
			ss.per(new char[5],4);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		
		return "success";
	}
	
	@ApiOperation(value="插入链接", notes="插入源链接和短链接对应关系入库") 
	@ApiImplicitParams({
		@ApiImplicitParam(name = "shortUrl", value = "用户自定义短链接", required = false, dataType = "String"),
		@ApiImplicitParam(name = "sourceUrl", value = "源链接地址", required = true, dataType = "String")
	 })
	@PutMapping("InsertSourceUrlWhithShortUrl")
	public String insertSourceUrlWhithShortUrl(@RequestParam(name="shortUrl",required=false)String shortUrl,
										 	   @RequestParam(name="sourceUrl",required=true)String sourceUrl){
		if(shortUrl != null || !"".equals(shortUrl)){
			return 	ssi.insertSourceUrlWhithShortUrl(shortUrl, sourceUrl);
		}
		
		return ssi.insertSourceUrlWhithShortUrlOParm(sourceUrl);
	};
	
	@ApiOperation(value="短链接转跳源链接", notes="根据短链接转跳源链接") 
	@ApiImplicitParam(name = "shortUrl", value = "短链接地址", required = true, dataType = "String")
	@GetMapping("{shortUrl}")
	public ModelAndView toSourceUrl(@PathVariable(name="shortUrl",required=true)String shortUrl){
		String str = ssi.selectShortUrlUrlBySource(shortUrl);
		if(str != null || "".equals(str))
		return new ModelAndView(new RedirectView(str)); 
		
		return new ModelAndView(new RedirectView(url));
		
	};
	
	@ApiOperation(value="短链接转跳源链接", notes="根据短链接获取源链接") 
	@ApiImplicitParam(name = "shortUrl", value = "短链接地址", required = true, dataType = "String")
	@GetMapping()
	public String GetSourceUrl(@RequestParam(name="shortUrl",required=true)String shortUrl){
		String str = ssi.selectShortUrlUrlBySource(shortUrl);
		if(str != null || "".equals(str))
		return str; 
		
		return "短链接有误";
		
	};
}
